// FactoryPatternModule.java
import java.util.*;

interface AgriculturalMachine {
    void operate();
    String getStatus();
    void setLocation(String location);
}

class Seeder implements AgriculturalMachine {
    private String location = "未定位";
    
    @Override
    public void operate() {
        System.out.println("播种机正在播种...");
    }
    
    @Override
    public String getStatus() {
        return "播种机 - 位置: " + location;
    }
    
    @Override
    public void setLocation(String location) {
        this.location = location;
    }
}

class Sprayer implements AgriculturalMachine {
    private String location = "未定位";
    
    @Override
    public void operate() {
        System.out.println("喷药机正在喷洒农药...");
    }
    
    @Override
    public String getStatus() {
        return "喷药机 - 位置: " + location;
    }
    
    @Override
    public void setLocation(String location) {
        this.location = location;
    }
}

class Harvester implements AgriculturalMachine {
    private String location = "未定位";
    
    @Override
    public void operate() {
        System.out.println("收割机正在收割作物...");
    }
    
    @Override
    public String getStatus() {
        return "收割机 - 位置: " + location;
    }
    
    @Override
    public void setLocation(String location) {
        this.location = location;
    }
}

class MachineFactory {
    public AgriculturalMachine createMachine(String type) {
        return switch (type.toLowerCase()) {
            case "seeder" -> new Seeder();
            case "sprayer" -> new Sprayer();
            case "harvester" -> new Harvester();
            default -> throw new IllegalArgumentException("未知农机类型: " + type);
        };
    }
}

public class FactoryPatternModule {
    private static final Scanner scanner = new Scanner(System.in);
    private static final List<AgriculturalMachine> machines = new ArrayList<>();
    
    public static void execute() {
        while (true) {
            System.out.println("\n===== 农机设备管理 =====");
            System.out.println("1. 添加农机");
            System.out.println("2. 查看农机状态");
            System.out.println("3. 设置农机位置");
            System.out.println("4. 返回主菜单");
            System.out.print("请选择操作(1-4): ");
            
            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗换行符
            
            switch (choice) {
                case 1 -> addMachine();
                case 2 -> viewMachineStatus();
                case 3 -> setMachineLocation();
                case 4 -> { return; }
                default -> System.out.println("无效选择，请重新输入");
            }
        }
    }
    
    private static void addMachine() {
        System.out.print("请输入农机类型(seeder/sprayer/harvester): ");
        String type = scanner.nextLine();
        
        MachineFactory factory = new MachineFactory();
        try {
            AgriculturalMachine machine = factory.createMachine(type);
            machines.add(machine);
            System.out.println(type + " 添加成功!");
        } catch (IllegalArgumentException e) {
            System.out.println("错误: " + e.getMessage());
        }
    }
    
    private static void viewMachineStatus() {
        if (machines.isEmpty()) {
            System.out.println("没有可用的农机设备");
            return;
        }
        
        System.out.println("\n=== 农机状态 ===");
        for (int i = 0; i < machines.size(); i++) {
            System.out.println((i+1) + ". " + machines.get(i).getStatus());
        }
    }
    
    private static void setMachineLocation() {
        viewMachineStatus();
        if (machines.isEmpty()) return;
        
        System.out.print("选择要设置位置的农机编号: ");
        int index = scanner.nextInt() - 1;
        scanner.nextLine();  // 消耗换行符
        
        if (index < 0 || index >= machines.size()) {
            System.out.println("无效的农机编号");
            return;
        }
        
        System.out.print("请输入位置信息: ");
        String location = scanner.nextLine();
        machines.get(index).setLocation(location);
        System.out.println("位置更新成功");
    }
    
    public static List<AgriculturalMachine> getMachines() {
        return machines;
    }
}